import {
	createRouter,
	createWebHashHistory
} from "vue-router"

import { getTokenLocal } from "@/utils/auth.js"
import {
	showConfirmDialog
} from 'vant';


// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [{
		path: "/",
		redirect: "/home"
	},
	/*{
		path: "/studentDiaryList",
		name: "studentDiaryList",
		component: () => import("@/views/student/studentDiaryList.vue"),
	},*/
	{
		path: "/home",
		name: "home",
		component: () => import("@/views/home/home.vue"),
		meta: {
			title: "首页"
		},
	},
	{
		path: "/product",
		name: "product",
		component: () => import("@/views/product/product.vue"),
		meta: {
			title: "商品"
		},
	},
	{
		path: "/productDetail/:id", //动态路由
		name: "productDetail",
		component: () => import("@/views/product/productDetail.vue"),
		meta: {
			title: "商品详情"
		},
	},
	{
		path: "/shopcart",
		name: "shopcart",
		component: () => import("@/views/shopcart/shopcart.vue"),
		meta: {
			title: "购物车",
			requireAuth: true, //需要Token
		},
	},
	{
		path: "/user",
		name: "user",
		component: () => import("@/views/user/user.vue"),
		meta: {
			title: "我的"
		},
	},

	{
		path: "/login",
		name: "login",
		component: () => import("@/views/user/login.vue"),
		meta: {
			title: "登录"
		},
	},

	{
		path: "/register",
		component: () => import("@/views/user/register.vue"),
		name: "register",
		meta: {
			title: "用户注册"
		}
	},

	{
		path: "/profile",
		component: () => import("@/views/user/profile.vue"),
		name: "profile",
		meta: {
			title: "个人资料",
			requireAuth: true, //需要Token
		}
	},

	{
		path: "/updatePwd",
		component: () => import("@/views/user/updatePwd.vue"),
		name: "updatePwd",
		meta: {
			title: "密码修改",
			requireAuth: true, //需要Token
		}
	},

	{
		path: "/orderCreate",
		component: () => import("@/views/order/orderCreate.vue"),
		name: "orderCreate",
		meta: {
			title: "创建对单",
			requireAuth: true, //需要Token
		}
	},

	{
		path: "/addressList",
		component: () => import("@/views/address/addressList.vue"),
		name: "addressList",
		meta: {
			title: "收货地址列表",
			requireAuth: true
		}
	},

	{
		path: "/addressEdit/:id",
		component: () => import("@/views/address/addressEdit.vue"),
		props: true,
		name: "addressEdit",
		meta: {
			title: "收货地址",
			requireAuth: true
		},
	},
	
	{
		path: "/ShareList",
		component: () => import("@/views/share/ShareList.vue"),
		props: true,
		name: "ShareList",
		meta: {
			title: "新闻分享",
			requireAuth: true
		},
	},
	
	{
		path: "/messageList",
		component: () => import("@/views/message/messageList.vue"),
		props: true,
		name: "messageList",
		meta: {
			title: "留言反馈",
			requireAuth: true
		},
	},
	
	{
		path: "/VideoList",
		component: () => import("@/views/video/VideoList.vue"),
		props: true,
		name: "VideoList",
		meta: {
			title: "视频专区",
			requireAuth: true
		},
	},
	
	{
		path: "/contact",
		component: () => import("@/views/home/contact.vue"),
		props: true,
		name: "contact",
		meta: {
			title: "联系我们",
			requireAuth: true
		},
	},

	{
		path: "/orderList",
		component: () => import("@/views/order/orderList.vue"),
		name: "orderList",
		meta: {
			title: "我的订单",
			requireAuth: true
		},
	},

	{
		path: "/orderDetail/:id",
		component: () => import("@/views/order/orderDetail.vue"),
		props: true,
		name: "orderDetail",
		meta: {
			title: "收订单详情",
			requireAuth: true
		},
	},	
	
	

	{
		path: "/working",
		component: () => import("@/views/working.vue"),
		name: "working",
		meta: {
			title: "努力中...."
		},
	},
	{
		path: "/:pathMatch(.*)",
		name: "notFound",
		component: () => import('@/views/notFound.vue'),
		meta: {
			title: "导航页面不存在"
		},
	},

]

// 3. 创建路由实例并传递 `routes` 配置
const router = createRouter({
	// 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
	history: createWebHashHistory(),
	routes, // `routes: routes` 的缩写
})


router.beforeEach((to, from, next) => {
	if (to.name === 'login') { // 路由条状到login的，直接放行
		console.log(`${from.name}跳转到login`); // 控制台调试输出
		next(); // 路由继续往下跳转
		return;
	}

	if (!to.meta.requireAuth) { // 路由不需要验证的，直接放行
		console.log(`${from.name}跳转到${to.name},目的路由不需要用户认证`); // 控制台调试输出
		next();
		return;
	}

	// 路由需要验证的   
	const token = getTokenLocal();
	if (!token) { // 如果token不存在的，则跳转登录login
		console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token不存在！强制跳转到login`); // 控制台调试输出

		showConfirmDialog({
			message: "没有Token,需要重新登录吗?",
		}).then(() => {
			// 需要登录，则跳转到登录login，并传递参数(登录后跳转的路由)
			next({
				name: 'login',
				query: { redirect: to.path }
			});
		}).catch(() => {
			// 不需要登录，跳转到首页            
			router.push({ name: "home" });
		});
		return;
	}

	console.log(`${from.name}跳转到${to.name},目的路由需要用户认证，Token也存在！`); // 控制台调试输出
	// 其余情况，则路由放行
	next(); //
});

export default router